$.fn.autocomplete = function(url,option){
    var me= this;
    var strKey= $(me).val();
    var strKeyBak= "";
    var isShow = false;
    var doption={
        iwidth: '0px',    //下拉框的宽度
        iLengthLower: 1,    //当表单的长度大于iLengthLower小于iLengthUpper时才开始执行搜索
        iLengthUpper: 50,
        strPara: "q",    //变量名称
        zIndex: 1,    //提示框的Z-INDEX值
        hasscroll: 0,    //是否出现滚动条0无1有
        hasclose: 1,    //是否拥有关闭窗口
        desfun:function(){}
    };
    
    $.extend(doption,option);    
    var iLengthLower= doption.iLengthLower;
    var iLengthUpper= doption.iLengthUpper;
    var strPara= doption.strPara;
    //if ($("#autocomplete").length<1){$("body").append("<div id='autocomplete' class='autocompletefloor'></div>");}
    if ($(".gac_m").length<1){$("body").append("<table class='gac_m' cellspacing='0' cellpadding='0'></table>");}
    //$("#autocomplete").hide();
    $(".gac_m").css("visibility","hidden");
    
    $(me).keyup(function(e){keysearch(e.keyCode);});
    $(me).keydown(function(e){KeyEvent(e.keyCode);});
    $(me).bind("blur",function(){
        //strKeyBak= $("#autocomplete ul .selected .keyname").text();    //为click事件增加处理
        strKeyBak= $(".gac_m .gac_b .gac_c").text();    //为click事件增加处理
        if (strKeyBak.length>0 && strKeyBak!=$(me).val()){
            $(me).val(strKeyBak);
            doption.desfun();
        };
        floorHide();
    });
    
    var encode=function(v){//如果包含中文就escape,避免重复escape)
  return escape(v).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
 }
    
    function floorHide(){
        //$("#autocomplete").hide().html("");
        $(".gac_m").css("visibility","hidden");
        strKey= "";
        isShow = false;
    }
    
    function floorShow(){
        var p= $(me).offset();
        var w= (doption.iwidth == "0px") ? $(me).outerWidth() : doption.iwidth;
        $(".gac_m").css({
        //'z-index:':doption.zIndex,
        width:w,
        top:parseInt(p.top+$(me).outerHeight()-1)+"px",
        left:parseInt(p.left)+"px"
    //}).show();
    }).css("visibility","visible");
        strKey= "";
        isShow = true;
    }
    
    function keysearch(code){
        var strKeyNow=$(me).val();
        if(code == 38 || code == 40 || code == 13 || code == 27 || code == 9) return;    //TAB/回车、ESC、向上、向下
        if((strKey == "" || strKeyNow != strKey) && strKeyNow.length >= iLengthLower && strKeyNow.length <= iLengthUpper){
            $.ajax({
                type: "Get",
                dataType: "json",
                url: url,
                data: strPara != "" ? strPara + "=" + encode(strKeyNow) : "",
                success: function(json){
                    json=json.keylist;
                    if (json.length>0){
                        //获取搜索数据
                        //var strContent= "<ul>";
                        var strContent= "<tbody>";
                        $.each(json, function(i, n){
                            if(n.keyname.length>0){    //如果
                               //alert(n.keyname);
                                //strContent+= '<li class="keyinfo"><span class="keyname">'+n.keyname+'</span>';
                                strContent+= '<tr class="gac_a"><td class="gac_c">'+n.keyname+'</td>';
                                try{
									//if (n.keyextend.length>0){strContent+='<span class="keyextend">'+n.keyextend+'</span>';}
									if (n.keyextend.length>0){strContent+='<td class="gac_d">'+n.keyextend+'</td>';}
                                }catch(e){
									strContent+='<td class="gac_d"></td>';
								};
                                //strContent+= '</li>';
                                strContent+= '</tr>';
                           };
                        });
                        //strContent+='</ul>';
                        strContent+='</tbody>';
                        if (doption.hasclose==1){
                            //strContent+= '<li class="close"><span>关闭</span></li>';
                            strContent+= '<tfoot><tr><td></td><td class="gac_e">关闭</td></tr></tfoot>';
                        }
                        //$("#autocomplete").html(strContent);
                        $(".gac_m").html(strContent);
                        //$("#autocomplete .keyinfo").mouseover(function(){
                        $(".gac_m tbody tr").mouseover(function(){
							//$("#autocomplete .selected").removeClass("selected");
							$(".gac_m .gac_b").removeClass("gac_b");
							//$(this).removeClass("unselected").addClass("selected");
							$(this).removeClass("gac_a").addClass("gac_b");
						}).mouseout(function(){
							//$(this).removeClass("selected").addClass("unselected");
							$(this).removeClass("gac_b").addClass("gac_a");
						}).click(function(){
							if(strKeyBak.length()>0){
								$(me).val(strKeyBak);
							}
						});
                        floorShow();
                    }else{
                        //没有搜索数据
                        floorHide();
                        return;
                    }
                }
            });
            strKey=$(me).val();
        }
        if(strKey.length == 0 || strKey.length <= iLengthLower || strKey.length >= iLengthUpper) floorHide();
    }
    
    function KeyEvent(code){
        if(code == 27){floorHide();};//回车键、ESC键
        if(code == 13){floorHide();doption.desfun();};
        if(!isShow) return;
        //ObjSelected=$("#autocomplete ul .selected");
        ObjSelected=$(".gac_m .gac_b");
        if (ObjSelected.length>0){    //如果已经有选定
            //alert('dasfdas');
            if(code == 38){    //向上键
                if(ObjSelected.prev().text() != ""){    //如果不是第一个数据
                    //ObjSelected.removeClass("selected").addClass("unselected").prev().removeClass("unselected").addClass("selected");
                    ObjSelected.removeClass("gac_b").addClass("gac_a").prev().removeClass("gac_a").addClass("gac_b");
                    //$(me).val($("#autocomplete ul .selected .keyname").text());
                    $(me).val($(".gac_m .gac_b .gac_c").text());
                }else{
                    //ObjSelected.removeClass("selected").addClass("unselected");
                    ObjSelected.removeClass("gac_b").addClass("gac_a");
                    //$("#autocomplete .keyinfo:last").removeClass("unselected").addClass("selected");
                    $(".gac_m tbody tr:last").removeClass("gac_a").addClass("gac_b");
                    //$(me).val($("#autocomplete ul .selected .keyname").text());
                    $(me).val($(".gac_m .gac_b .gac_c").text());
                }
            }else if (code == 40){    //向下键
                if(ObjSelected.next().text() != ""){    //如果不是第一个数据
                    //ObjSelected.removeClass("selected").addClass("unselected").next().removeClass("unselected").addClass("selected");
                    ObjSelected.removeClass("gac_b").addClass("gac_a").next().removeClass("gac_a").addClass("gac_b");
                    //$(me).val($("#autocomplete ul .selected .keyname").text());
                    $(me).val($(".gac_m .gac_b .gac_c").text());
                }else{
                    //ObjSelected.removeClass("selected").addClass("unselected");
                    ObjSelected.removeClass("gac_b").addClass("gac_a");
                   //$("#autocomplete .keyinfo:first").removeClass("unselected").addClass("selected");
                    $(".gac_m tbody tr:first").removeClass("gac_a").addClass("gac_b");
                    //$(me).val($("#autocomplete ul .selected .keyname").text());
                    $(me).val($(".gac_m .gac_b .gac_c").text());
                }
            }
        }else if(code == 38){
                //$("#autocomplete .keyinfo:last").removeClass("unselected").addClass("selected");
                $(".gac_m tbody tr:last").removeClass("gac_a").addClass("gac_b");
                //$(me).val($("#autocomplete .keyinfo:last .keyname").text());
                $(me).val($(".gac_m tbody tr:last .gac_c").text());
        }else if(code == 40){
                //$("#autocomplete .keyinfo:first").removeClass("unselected").addClass("selected");
                $(".gac_m tr:first").removeClass("gac_a").addClass("gac_b");
                //$(me).val($("#autocomplete .keyinfo:first .keyname").text());
                $(me).val($(".gac_m tbody tr:first .gac_c").text());
        }
    }
}